package eip.chapter4.invalidmessage.mule;

import org.mule.umo.UMOFilter;
import org.mule.umo.UMOMessage;

import eip.chapter4.invalidmessage.HotelSearchMessage;

/**
 * This filter checks whether the supplied message is a valid one. If
 * the message is valid this filter return true. If it's not a valid
 * one this filter returns false
 *  
 * @author nl24167
 *
 */
public class HotelSearchFilter implements UMOFilter {

	/**
	 * This filter will be applied after the transformation has occured.
	 * In the transformation we already transform the incoming XML file
	 * to a java object.
	 * 
	 * The message is only valid if all fields are filled in.
	 */
	public boolean accept(UMOMessage message) {
		if (message.getPayload() instanceof HotelSearchMessage) {
			HotelSearchMessage searchMessage = (HotelSearchMessage) message.getPayload();
			if (searchMessage.getArrivalDate()!=null &&
				searchMessage.getDepartureDate()!=null &&
				searchMessage.getCity() != null && 
				searchMessage.getCity().length() > 0 &&
				searchMessage.getCountry() != null && 
				searchMessage.getCountry().length() > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			// it's not a HotelSearchMessage thus invalid
			return false;	
		}	
	}
}
